<%@tag description="descricao" pageEncoding="UTF-8"%>

<%@ taglib uri="/WEB-INF/struts-logic" prefix="logic" %>
<%@ taglib uri="/WEB-INF/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/el" prefix="el" %>
<%@ taglib uri="/WEB-INF/c" prefix="c"  %>

<%@ attribute name="url" required="true" %>
<%@ attribute name="searchResultName" required="true" %>
<%@ attribute name="pageSize" required="false" %> <!-- nao foi usado -->
<%@ attribute name="pageInfo" required="false" %>
<%@ attribute name="maxPagesIndex" required="false" %>
<!--
    Tag de Paginação    
--> 

<bean:define name="${searchResultName}" toScope="request" id="SEARCH_RESULT"/> 
<bean:define name="SEARCH_RESULT" property="pagingBean" id="PG_BEAN"/>

<bean:define id="SHOW_PAGGING_TAG">${SEARCH_RESULT.resultSize != 0}</bean:define>

<logic:equal name="SHOW_PAGGING_TAG"  value='true'> 

<!-- Verifica se possui os atributos de paginacao -->
<logic:present name="PG_BEAN">
<logic:equal name="PG_BEAN" property="enabled" value="true"> 

    <bean:define name="PG_BEAN" property="pageNumber" id="PG_NUMBER"/>
    <bean:define name="PG_BEAN" property="lastPageNumber" id="PG_TOTAL_PAGINAS"/>
    <bean:define name="PG_BEAN" property="countRows" id="PG_ITENS_TOTAL"/>    
    <bean:define name="PG_BEAN" property="pageRequestQuery" id="PG_REQUEST_QUERY"/>
     
       <!-- se passou o page size no atributo da tag-->
      
     <!-- se nao passou o result size, captura o page size default do bean-->
     <logic:notPresent  name="pageSize" > 
	      <bean:define name="PG_BEAN" property="pageSize" id="PG_SIZE"/>    
     </logic:notPresent>  

    <table align="center" border="0" cellpadding="1" cellspacing="0" width="100%"><tbody><tr>
        <logic:notEqual name="pageInfo" value="false">
	    <td nowrap="nowrap" width="75%">
            <font class="txt-paging">               
                Página ${PG_NUMBER} de ${PG_TOTAL_PAGINAS} 
                <logic:equal name="PG_TOTAL_PAGINAS" value="1">Página</logic:equal>
                <logic:notEqual name="PG_TOTAL_PAGINAS" value="1">Páginas</logic:notEqual>.
                 Itens:${PG_BEAN.firstRowInPage + 1} a ${PG_BEAN.firstRowInPage +SEARCH_RESULT.resultSize} - Total: ${PG_ITENS_TOTAL} Itens.
            </font>
        </td>
        </logic:notEqual>
            
	    <td valign="center" width="10%">       
            <table border="0" width="100%"><tbody><tr>            
                <td>
     
	   <bean:define  id="pageUrl" value="${url}?${PG_REQUEST_QUERY}" />   

	        <logic:notEqual name="PG_BEAN" property="pageNumber" value="1" >
	                <a href="${pageUrl}&pageNumber=${PG_BEAN.firstPageNumber}">
	                    <img src="${contexto}/pages/images/seta2a.gif" border="0" height="13" width="13"></a>                    
	        </logic:notEqual>
	                </td>
	                <td>
	         <logic:equal name="PG_BEAN" property="hasBack" value="true" >
	                    <a href="${pageUrl}&pageNumber=${PG_BEAN.backPage}" >
	                    <img src="${contexto}/pages/images/seta1a.gif" border="0" height="13" width="13"></a>
	        </logic:equal>
	                </td>
	            </tr></tbody></table>
	        </td>
	        
	        
	         <logic:notEqual name="PG_TOTAL_PAGINAS"  value="1" >   
	        <td valign="center" width="5%"> 
	       
	        <table border="0" width="100%"><tbody><tr><td>
	        		<div id="divSelectPage" > 
	        		</div>
	            </td>	           
	            </tr></tbody></table>
	            <script type="text/javascript">				
		           	            	
	            	function createPageCombo(){
	            		var div = document.getElementById("divSelectPage");
	            		var total = new Number(${PG_TOTAL_PAGINAS});
            			var element = '<select id="pageNumberSelect" ';
            			element += 'onchange="javascript:goPageSelected();" >';
	            		for(var i = 1; i <= total; i++){
	            			if(i == ${PG_NUMBER}){
		            			element +='<option selected="selected">' + i + '</option>';
	            			}else{
		            			element +='<option value="' + i + '" >'+ i+ '</option>';
		            		}
	            			
	            		}
						element += '</select>';
						div.innerHTML = element;
	            		
	            	}
	            	createPageCombo();
	            	
	            	function goPageSelected(){		       
	            	      
		            	var select = document.getElementById("pageNumberSelect");
						var pageNumber =  select[select.selectedIndex].value;
						if(pageNumber < 1){
							 pageNumber = 1;							 
						}else if(pageNumber > ${PG_TOTAL_PAGINAS}){					
							pageNumber = ${PG_TOTAL_PAGINAS};
						}							
            	   		window.location = '${pageUrl}&pageNumber=' +pageNumber ;
		            }
		            
	            	function createPageInput(){
	            		var div = document.getElementById("divSelectPage");
	            		var total = new Number(${PG_TOTAL_PAGINAS});
            			var element = '<input type="text" id="pageNumberInput" size="1" ';
            			element += 'onkeydown="if(event.keyCode == 13) goPageInput();" />';
            			
						div.innerHTML = element;
	            		
	            	}
	            	//createPageInput();

					function goPageInput(){		               
		            	var select = document.getElementById("pageNumberInput");
						var pageNumber =  select.value;
						alert("==" + pageNumber);
						if(pageNumber < 1){
							 pageNumber = 1;							 
						}else if(pageNumber > ${PG_TOTAL_PAGINAS}){					
							pageNumber = ${PG_TOTAL_PAGINAS};
						}							
            	   		window.location = '${pageUrl}&pageNumber=' +pageNumber ;
		            }
	            	 
	            
	            </script>
	        </td>
	          </logic:notEqual>
	        
	     	     	        
	        <td valign="center" width="10%"> 
	            <table border="0" width="100%"><tbody><tr>
	                <td>
	        <logic:equal name="PG_BEAN" property="hasNext" value="true" >
	                    <a href="${pageUrl}&pageNumber=${PG_BEAN.nextPage}" >
	                     <img src="${contexto}/pages/images/seta1.gif" border="0" height="13" width="13"></a>
	        </logic:equal>
	                </td>
	                <td>
	        <logic:notEqual name="PG_BEAN" property="pageNumber" value="${PG_TOTAL_PAGINAS}" >
	                    <a href="${pageUrl}&pageNumber=${PG_TOTAL_PAGINAS}" border="0" height="13" width="13">
	                    <img src="${contexto}/pages/images/seta2.gif" border="0" height="13" width="13"></a>
	        </logic:notEqual>

                </td>
                
            </tr></tbody></table>
    </td></tr></table>
    
     
</logic:equal>
</logic:present>

</logic:equal>